﻿using System.Data;
using System.IO;
using Microsoft.Synchronization.Data;
using Newtonsoft.Json;
using Syncronization.REST;

namespace Syncronization.Common
{
    public class mfLYSyncResponse
    {
        #region Properties

        /// <summary>
        /// Gets or sets the batch count.
        /// </summary>
        /// <value>The batch count.</value>
        public int BatchCount { get; set; }

        /// <summary>
        /// Gets or sets the changes.
        /// </summary>
        /// <value>The changes.</value>
        public string Changes { get; set; }

        /// <summary>
        /// Gets or sets the name of the group.
        /// </summary>
        /// <value>The name of the group.</value>
        public string GroupName { get; set; }

        /// <summary>
        /// Gets or sets the max anchor.
        /// </summary>
        /// <value>The max anchor.</value>
        public byte[] MaxAnchor { get; set; }

        /// <summary>
        /// Gets or sets the new anchor.
        /// </summary>
        /// <value>The new anchor.</value>
        public byte[] NewAnchor { get; set; }

        /// <summary>
        /// Gets or sets the originator id.
        /// </summary>
        /// <value>The originator id.</value>
        public int OriginatorId { get; set; }

        /// <summary>
        /// Gets or sets the total changes.
        /// </summary>
        /// <value>The total changes.</value>
        public int TotalChanges { get; set; }

        /// <summary>
        /// Gets or sets the total changes applied.
        /// </summary>
        /// <value>The total changes applied.</value>
        public int TotalChangesApplied { get; set; }

        /// <summary>
        /// Gets or sets the total changes failed.
        /// </summary>
        /// <value>The total changes failed.</value>
        public int TotalChangesFailed { get; set; }

        /// <summary>
        /// Gets or sets the total changes pending.
        /// </summary>
        /// <value>The total changes pending.</value>
        public int TotalChangesPending { get; set; }

        /// <summary>
        /// Gets or sets the total deletes.
        /// </summary>
        /// <value>The total deletes.</value>
        public int TotalDeletes { get; set; }

        /// <summary>
        /// Gets or sets the total inserts.
        /// </summary>
        /// <value>The total inserts.</value>
        public int TotalInserts { get; set; }

        /// <summary>
        /// Gets or sets the total updates.
        /// </summary>
        /// <value>The total updates.</value>
        public int TotalUpdates { get; set; }

        #endregion

        #region public

        /// <summary>
        /// Gets the sync context.
        /// </summary>
        /// <param name="syncResponse">The sync response.</param>
        /// <param name="groupMetadata">The group metadata.</param>
        /// <param name="serializer">The serializer.</param>
        /// <returns></returns>
        public static SyncContext GetSyncContext(mfLYSyncResponse syncResponse, SyncGroupMetadata groupMetadata,
                                                 JsonSerializer serializer)
        {
            var syncContext = new SyncContext();

            //deserialize dataset and add to syncContext
            serializer.Converters.Add(new DataSetConverter());

            using (var sr = new StringReader(syncResponse.Changes))
            {
                using (var reader = new JsonTextReader(sr))
                {
                    var dataSet = (DataSet) serializer.Deserialize(reader, typeof (DataSet));
                    syncContext.DataSet = dataSet;
                }
            }

            syncContext.OriginatorId = syncResponse.OriginatorId;

            syncContext.NewAnchor = new SyncAnchor(syncResponse.NewAnchor);
            syncContext.MaxAnchor = new SyncAnchor(syncResponse.MaxAnchor);

            syncContext.GroupProgress = new SyncGroupProgress(groupMetadata, syncContext.DataSet);

            return syncContext;
        }

        #endregion
    }
}